package com.chima.service.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import com.chima.dao.DepartmentFinaceDao;
import com.chima.dao.RateDao;
import com.chima.domain.AllRewardPercent;
import com.chima.domain.DepartJobfinace;
import com.chima.domain.Department;
import com.chima.domain.DepartmentCoffer;
import com.chima.domain.DepartmentFinace;
import com.chima.domain.Employees;
import com.chima.domain.Finace;
import com.chima.domain.FinacePercent;
import com.chima.domain.LeaderReward;
import com.chima.domain.MiddleEmpRate;
import com.chima.domain.MiddleReward;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.RewardStock;
import com.chima.service.DepartmentFinaceService;

public class DepartmentFinaceServiceImp implements DepartmentFinaceService {
	
	private DepartmentFinaceDao departmentFinaceDao;
	private RateDao rateDao;
	
	public void setRateDao(RateDao rateDao) {
		this.rateDao = rateDao;
	}

	public void setDepartmentFinaceDao(DepartmentFinaceDao departmentFinaceDao) {
		this.departmentFinaceDao = departmentFinaceDao;
	}

	public PageResponseBean ListDeptFinace(PageRequestBean pageRequestBean) {
		
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		int total = departmentFinaceDao.findByTotalCount(pageRequestBean.getDate());
		// 获取分页查询条件
		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<DepartmentFinace> departmentFinaces=departmentFinaceDao.findByCriteria(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(departmentFinaces);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}
/**
 * 计算科室奖金
 */
	public void savaOrupdate(String date, Department dept,double reward,double allcore,double dc) {
		
		DepartmentFinace d=departmentFinaceDao.findDeptFinacebyDeptanddate(dept.getName(),date);
		double deptCoffer=dc;
		System.out.println(dept.getName()+"==dc="+dc+"========allcore====="+allcore);
		
			//departmentFinaceDao.FindDepartmentCofferByName(dept.getName()).getCoffe();
	    double totalCoffer=allcore;
	    	//departmentFinaceDao.countDepartmentcoffe();
		if(d==null){
			DepartmentFinace dd= new DepartmentFinace();
			dd.setDate(date);
			dd.setDepartment(dept.getName());
			dd.setDeptFinace(deptCoffer/totalCoffer*reward);
			departmentFinaceDao.save(dd);
		}else{
			d.setDeptFinace(deptCoffer/totalCoffer*reward);
			departmentFinaceDao.update(d);
		}
	}

	public double findDeptfiance(String date, String dept) {
		
		return departmentFinaceDao.deptfinace(dept, date);
	}

	public List<FinacePercent> ListFinacePercent() {
		return departmentFinaceDao.findFinacePercent();
	}

	public void savaFinacePercent(FinacePercent f) {
		
		departmentFinaceDao.savaFinacePercent(f);
	}

	public void updateFinacePercent(FinacePercent f) {
		
		departmentFinaceDao.updateFinacePercent(f);
		
	}
   public void updateDepartmentCoffer(DepartmentCoffer f) {
		
		departmentFinaceDao.updateDepartmentCoffer(f);
		
	}

	public void deleteFinacePercent(int id) {
		departmentFinaceDao.deleteFinacePercent(id);
		
	}

	public List<Finace> listFinace(String date) {
		
		return departmentFinaceDao.findFinace(date);
	}
/**
 * 计算一次分配奖金（院领导和普通职工部分）
 */
	public void savaOrupdateFinace(String name, String date,double finace) {
		Finace f=departmentFinaceDao.findFinaceByNnameAndDate(name, date);
		if(f!=null){
			f.setFinace(finace);
			departmentFinaceDao.updateFinace(f);
		}else{
			Finace ff =new Finace();
			ff.setName(name);
			ff.setDate(date);
			ff.setFinace(finace);
			departmentFinaceDao.savaFinace(ff);
		}
		
	}

	public List<LeaderReward> listLeaderFinace(String date) {
		
		return departmentFinaceDao.findLeaderFinace(date);
	}
/**
 * 计算院领导奖金
 */
	public void savaOrupdateLeadFinace(Employees e, String date, double finace) {
		
		double totalcoffer =departmentFinaceDao.countEmpcoffe(36,3);
		
		LeaderReward f=departmentFinaceDao.findLeaderRewardByNnameAndDate(e.getName(), date);
		
		if(f!=null){
			f.setFinace(e.getPostManager()/totalcoffer*finace);
			departmentFinaceDao.updateLeaderReward(f);
		}else{
			LeaderReward ff =new LeaderReward();
			ff.setName(e.getName());
			ff.setDate(date);
			ff.setFinace(e.getPostManager()/totalcoffer*finace);
			departmentFinaceDao.savaLeaderReward(ff);
		}
		
	}

	public List<MiddleReward> listMiddleReward(String date) {
		
		return departmentFinaceDao.findMiddleReward(date);
	}

	public void savaOrupdateMiddleFinace(Employees e, String date, double finace,double totalCoffer) {
		
		
        MiddleReward m=departmentFinaceDao.findMiddleRewardByNnameAndDate(e.getName(), date);
		MiddleEmpRate rate=rateDao.findByEmpNameAnddate(e.getName(), date);
		
		if(m!=null){
			m.setFinace(e.getFinaceCoffe()/totalCoffer*finace*rate.getGrade());
			departmentFinaceDao.updateMiddleReward(m);
		}else{
			MiddleReward ff =new MiddleReward();
			ff.setName(e.getName());
			ff.setDepartment(e.getDepartment().getName());
			ff.setDate(date);
			ff.setFinace(e.getFinaceCoffe()/totalCoffer*finace*rate.getGrade());
			departmentFinaceDao.savaMiddleReward(ff);
		}
	}

	public MiddleEmpRate finfMiddleEmpRate(String name, String date) {
		
		return rateDao.findByEmpNameAnddate(name, date);
	}

	public PageResponseBean ListDeptCoffer(PageRequestBean pageRequestBean) {
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		int total = departmentFinaceDao.findByDeptCoffeTotalCount();
		// 获取分页查询条件
		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<DepartmentCoffer> departmentCoffers=departmentFinaceDao.findDeptCoffer(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(departmentCoffers);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
		
	}

	public DepartmentFinace findDeptFinacebyDeptAnddate(String dept, String date) {
		
		return departmentFinaceDao.findDeptFinacebyDeptanddate(dept, date);
	}

	public List<AllRewardPercent> ListinFinacePercent() {
		
		return departmentFinaceDao.ListinFinacePercent();
	}

	public void updateinFinacePercent(AllRewardPercent a) {
		
		departmentFinaceDao.updateinFinacePercent(a);
	}

	public AllRewardPercent FindinFinacePercent() {
	
		return departmentFinaceDao.FindinFinacePercent();
	}

	public List<RewardStock> listRewardStock() {
		
		return departmentFinaceDao.listRewardStock();
	}

	public void editRewardStock(RewardStock r) {
		
		departmentFinaceDao.upRewardStock(r);
	}

	public void savaRewardStock(RewardStock r) {

			departmentFinaceDao.saveRewardStock(r);
		
	}

	public RewardStock findRewardStock(String year) {
		
		return departmentFinaceDao.findRewardStock(year);
	}

	public double FindfinaceBydateAndName(String date,String name) {
		
		return departmentFinaceDao.FindfinaceBydateAndName(date, name);
	}

	public PageResponseBean ListListDepartJobfinace(PageRequestBean pageRequestBean) {
		
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
//		int total = departmentFinaceDao.findByDeptCoffeTotalCount();
		// 获取分页查询条件
		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<DepartJobfinace> departJobfinaces=departmentFinaceDao.ListListDepartJobfinace(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(departJobfinaces);
//		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}

	public void savaorupdateDepartJobfinace(DepartJobfinace f) {
		
		DepartJobfinace d=departmentFinaceDao.findDepartJobfinacebyDate(f.getDate());
		if(d==null){
			departmentFinaceDao.saveDepartJobfinace(f);
		}else{
			d.setFinace(f.getFinace());
			departmentFinaceDao.updateDepartJobfinace(d);
		}
	}

	public DepartmentCoffer FindDepartmentCofferByName(String name) {
		
		return departmentFinaceDao.FindDepartmentCofferByName(name);
	}

	public double countDepartcoffe(int dept) {
		
		return departmentFinaceDao.countDepartcoffe(dept);
	}

	public List<DepartmentFinace> findDepartmentFinaceByDate(String date) {
		
		return departmentFinaceDao.findDepartmentFinaceByDate(date);
	}

	@Override
	public double finddeptcorebydateanddept(String searchdate, Department d) {
		return departmentFinaceDao.finddeptcorebydateanddept(searchdate,d);
	}

	

}
